package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.*;

public class CheckingAccount extends AbstractAccount {

	private double overdraft;
	
	public CheckingAccount(double amount, double overdraft) {
		super(amount);
		this.overdraft = overdraft;
	}

	@Override
	public int withdraw(double amount) throws OverDraftLimitExceededException, IllegalAmountArgumentException {
		if (amount <= 0 ) {
			throw new IllegalAmountArgumentException(amount);
		}
		if (maximumAmountToWithdraw() < amount) {
			throw new OverDraftLimitExceededException( 
					this.maximumAmountToWithdraw()-amount);
		}
		
		balance -= amount;
		return 0;
	}
	
	@Override
	public double maximumAmountToWithdraw() {
		return balance+overdraft;
	}


	@Override
	public String toString() {
		return "CheckingAccount Balance = " + balance + " overdraft = " + overdraft;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		long temp;
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft))
			return false;
		return true;
	}
	
}
